﻿using SFast.DB.Page;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SFast.DB
{
    public class PageinationModel
    {

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRowCount { get; private set; }
        /// <summary>
        /// 起始行索引号（从0开始）
        /// </summary>
        public int StartRowIndex { get; private set; }
        /// <summary>
        /// 页面大小
        /// </summary>
        public int PageSize { get; private set; }
        /// <summary>
        /// 页面索引号（从0开始）
        /// </summary>
        public int PageIndex { get; private set; }
        /// <summary>
        /// 页面编号（从1开始）
        /// </summary>
        public int PageNumber { get { return PageIndex + 1; } private set { PageIndex = value - 1; } }
        /// <summary>
        /// 总页面数 
        /// </summary>
        public int PageCount { get; private set; }
        /// <summary>
        /// 当前页面记录数
        /// </summary>
        public int Length { get; private set; }
        /// <summary>
        /// 是否可以进行分页（分页条件是PageCount > 1)
        /// </summary>
        public bool HasPagination { get; private set; }
        /// <summary>
        /// 是否支持上一页
        /// </summary>
        public bool HasPrevious
        {
            get
            {
                if (!HasPagination || PageCount <= 2) return false;
                return PageIndex != 0 && PageIndex != 1;
            }
        }
        /// <summary>
        /// 是否支持下一页
        /// </summary>
        public bool HasNext { get { return HasPagination && PageIndex < PageCount - 2; } }
        /// <summary>
        /// 是否首页
        /// </summary>
        public bool HasFirst
        {
            get
            {
                //return HasPagination && PageIndex > 0 && PageIndex <= PageCount - 1;
                return HasPagination && PageCount - PageIndex < PageCount;
            }
        }
        /// <summary>
        /// 是否末页
        /// </summary>
        public bool HasLast
        {
            get
            {
                if (!HasPagination)
                    return false;
                //if (PageCount == 2)
                //    return PageIndex == 0;
                //return PageIndex != PageCount - 2 || PageIndex != PageCount - 1;
                return PageCount - PageIndex != 1;
            }
        }

        public int RowCount { get; private set; }

        /// <summary>
        /// 数据
        /// </summary>
        public IEnumerable Data { get; private set; }

        /// <summary>
        /// 页面HTML
        /// </summary>
        public String PageHtml { get; set; }

        public PageinationModel(IPagination collection)
        {
            this.PageSize = collection.PageSize;
            PageIndex = collection.PageIndex;
            Data = collection;
            RowCount = collection.RowCount;
            PageNumber = collection.PageNumber;  //当前页从1开始
            StartRowIndex = collection.StartRowIndex;
            TotalRowCount = collection.TotalRowCount;   //数据总行数
            PageCount = collection.PageCount;   //页面总数
            HasPagination = collection.HasPagination;  //是否分页
            this.PageHtml = collection._ToPageHtml();
            Length = collection.Length;  //当前页总数
        }

    }
}
